﻿using DimensionData.Data2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.PM
{
    /// <summary>
    /// 工作表,
    /// 工作项是行，阶段是列
    /// </summary>
    [Serializable]
    public class WorkSheet
    {
        /// <summary>
        /// 工作表编号,Guid.Empty和null视为无效
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Caption { get; set; }

        /// <summary>
        /// 变更集号
        /// </summary>
        public int Changeset { get; set; }

        /// <summary>
        /// 当前的工作项类别
        /// </summary>
        public WorkItemCategoryRef WorkItemCategory { get; set; }

        ///// <summary>
        ///// 工作项的多维树
        ///// </summary>
        //public WorkItemTree WorkItemTree { get; set; }
        /// <summary>
        /// 工作项引用
        /// </summary>
        public List<WorkItemRef> WorkItemCollection { get; set; }

        /// <summary>
        /// 工作阶段引用
        /// </summary>
        public List<WorkStageRef> WorkStageCollection { get; set; }

        public List<WorkSheetRow> RowCollection { get; set; }

        public bool IsNeesSave { get; set; }

        /// <summary>
        /// 多维文档
        /// </summary>
        public DimensionData.Data2.DDoc DDoc { get; set; }

        public WorkSheet()
        {
            WorkItemCollection = new List<WorkItemRef>();
            WorkStageCollection = new List<WorkStageRef>();


            this.RowCollection = new List<WorkSheetRow>();
        }

        /// <summary>
        /// 构建数据，根据工作项和阶段构建完整的数据
        /// </summary>
        public void BuildData()
        {
            List<DRow> yDRows = this.DDoc.YTree.Complanate2();
            List<DRow> xDRows = this.DDoc.XTree.Complanate2();

            for (int yIndex = 0; yIndex < yDRows.Count; yIndex++)
            {
                WorkSheetRow row = new WorkSheetRow();
                row.YDRow = yDRows[yIndex];
                for (int xIndex = 0; xIndex < xDRows.Count; xIndex++)
                {
                    DRow xDRow = xDRows[xIndex];
                    WorkSheetCell cell = new WorkSheetCell();
                    cell.RowCodePath = yDRows[yIndex].DPath;

                    cell.ColCodePath = xDRows[xIndex].DPath;
                    row.CellCollection.Add(cell);
                }

                this.RowCollection.Add(row);
            }
        }

        public void InsertRow(DRow yDRow, DRow insertYDRow)
        {
            List<DRow> xDRows = this.DDoc.XTree.Complanate2();
            WorkSheetRow row = new WorkSheetRow();
            row.YDRow = insertYDRow;
            for (int xIndex = 0; xIndex < xDRows.Count; xIndex++)
            {
                DRow xDRow = xDRows[xIndex];
                WorkSheetCell cell = new WorkSheetCell();
                cell.RowCodePath = insertYDRow.DPath;

                cell.ColCodePath = xDRows[xIndex].DPath;
                row.CellCollection.Add(cell);
            }

            for (int index = 0; index < this.RowCollection.Count; index++)
            {
                if (this.RowCollection[index].YDRow == yDRow)
                {
                    this.RowCollection.Insert(index, row);
                    break;
                }
            }
        }
    }
}
